local sk__yaozhi = fk.CreateSkill {

  name = "sk__yaozhi",

  tags = {},

}
local function getYaozhiSkills(player, event)
  local list = {
    ["phase_start"] = {
      --standard_ex
      "ex__luoshen", "ex__guanxing",
      --shzl
      "yinghun",
      --yj
      "nos__miji", "ty_ex__zhenjun", "ty_ex__qianxi", "fumian",
      --sp
      "shenzhi",
      --OL
      "xunzhi", "fujian", "ol__tongdu", "yongzu", "qin__zhangzheng",
      --tenyear
      "fuxue", "miyi", "cansi", "fensi", "huizhi", "silun", "jincui", "langxi", "shunshi", "mouni", "ty__huoshui",
      "xianwei",
      "yingyu", "dingji", "zhangrong", "fazhu", "wanglu", "hanying", "yanzhengn",
      --mobile
      "yingjian", "liezhi", "mobile__diaodu", "mou__luoshen",
      --overseas
      "os__qingkou", "os__zhiming", "os__tanfeng", "os_xing__zhilue", "os__shengxi", "os__fenming", "os__liexi",
      --jsrg
      "chaozheng", "jizhaoq", "zhuhuanh", "xuanhua", "guyin", "guitu", "qingzi", "js__niluan", "yaoyan", "js__zhimeng",
      --offline
      "rom__zhenglian", "chaojue",
      --qsgs
      --"qyt__dongcha",
      --sanguokill
      "sk__diaodu", "sk__huituo", "sk__caiyu", "sk_le__luoshen", "sk__tiance", "sk__wengua",
    },
    ["phase_finish"] = {
      --standard_ex
      "biyue", "ex__biyue",
      --shzl
      "jushou", "ol_ex__jushou", "zhengu", "zuilun", "meihun",
      --yj
      "ty_ex__zhenjun", "m_ex__jieyue", "nos__miji", "miji", "ty_ex__zishou", "ty_ex__jingce", "juece", "ty_ex__zhiyan",
      "os_ex__bingyi", "daiyan", "huimin",
      "tw__liancai",
      --sp
      "mozhi", "fk__yuanhu", "bifa", "kuiwei", "kunfen",
      --OL
      "ty__tunjiang", "kuanshi", "fujian", "lanjiang", "zhangjiq", "chenshuo", "jieli", "shenfu", "lizhan",
      --tenyear
      "sp__youdi", "jue", "pijing", "zhuihuan", "nuanhui", "shijiz", "zhenxing", "guanyue", "ty__jieying", "yizhengc",
      "nifu",
      "ty__qinqing", "suizheng", "ld__shengxi", "haoyi", "sangu", "linghui",
      --mobile
      "chengzhao", "shangjian", "mobile__guixiu", "tianyin", "mou__jieyue", "mou__shipo",
      --overseas
      "os__fenwu", "os__zhiqu", "os__xingluan", "os__qiaosih", "os__juchen", "os__xingbu",
      --jsrg
      "zhaobing", "jizhaoq", "js__cangchu", "js__chuanxin", "js__pianchong",
      --mini
      "mini_zhuixi", "mini_lvyuan",
      --offline
      "shude",
      --qsgs
      "qyt__guixin", "qyt__hujia", "qyt__gongmou", "qw__longluo",
      --sanguokill
      "sk__zuilun", "sk__diancai", "sk__yinyuan", "sk__kuizhu", "sk__qinqing", "sk__chenyu", "sk__huoshui",
    },
    ["phase_play"] = {
      --standard_ex
      "qingnang", "lijian", "hs__kurou",
      "ex__rende", "ex__yijue", "jijie", "ex__zhiheng", "ex__fanjian", "ex__guose", "ex__jieyin", "ex__chuli",
      --shzl
      "ol_ex__qiangxi", "quhu", "tianyi", "ol_ex__luanji", "ol_ex__dimeng", "ol_ex__tiaoxin", "feijun", "jueyan",
      "os__gongxin", "poxi", "ol_ex__changbiao",
      --yj
      "ty_ex__sanyao", "nos__jujian", "ty_ex__jianyan", "m_ex__ganlu", "m_ex__xianzhen", "ty_ex__mingce", "qice",
      "m_ex__anxu",
      "ty_ex__gongqi", "junxing", "m_ex__junxing", "m_ex__mieji", "mingjian", "ty_ex__anguo", "ty_ex__wurong",
      "ty_ex__huaiyi",
      "m_ex__yanzhu", "duliang", "zhige",
      "tw__huzhu", "tw__rangyi",
      --sp
      "os__yuanhu", "xueji", "ol__xuehen", "mizhao", "ol__zhoufu", "qiangwu", "ol__fenxun", "xiemu", "ty_ex__quji",
      "dahe",
      "tanhu",
      --OL
      "ziyuan", "ty__lianzhu", "shanxi", "ty__lianji", "jianji", "shoufu", "guanxu", "liehou", "chongxin", "shengong",
      "qingyix", "xiaosi", "xianbi", "leijie", "yanru", "chanshuang", "shandao", "yichengl", "zuolian", "jinlan",
      "hunjiang",
      "weimian", "shidu", "choufa", "yanxi", "xuanbei", "yushen", "bolong", "fuxun", "lilun", "yuanlue",
      "qin__bianfa", "qin__qihuo", "qin__youmie",
      --tenyear
      "guolun", "ty_ex__jixu", "kuiji", "jiuxianc", "changqu", "jianguo", "zhuren", "busuan", "xiangmian", "daoshu",
      "ty__jianji",
      "wencan", "jianzheng", "mansi", "caizhuang", "huahuo", "xinyou", "huiji", "zunwei", "jichun", "qiongying", "tanbei",
      "ty__xiongsuan", "lueming", "tunan", "yanjiao", "ty__songshu", "os__mouzhu", "libang", "nuchen", "ty__wuyuan",
      "huoqi", "xianshou", "minsi", "xuezhao", "ty__qingcheng", "channi", "ty__kuangfu", "qingtan", "jinhui", "cuijian",
      "ty__jianshu", "yingshui", "zigu", "ty__beini", "midu", "qiangzhiz", "ty__fenglue", "boyan", "weimeng", "chiying",
      "luecheng",
      "kuizhen", "sirui", "liangyan", "wuwei", "kuangzhan", "tongwei", "weiwu", "ol__zhongjian", "ty__qimei", "xuzhi",
      "beiyu",
      "bianzhuang", "shuangbi", "lisao", "zhongyanz",
      --mobile
      "pingcai", "qiaosi", "zhengjing", "zhilve", "guli", "huiyao", "mobile__yufeng", "beizhu", "poxiang", "zhujian",
      "duansuo",
      "yizheng", "hannan", "mobile__daoshu", "wisdom__qiai", "shameng", "jianzhan", "ofl__huishi", "mobile__yinju",
      "nos__cunsi",
      "luanchou", "dingzhou", "shanxie", "zundi", "mobile__shangyi", "yangjie",
      "mou__jushou", "mou__xianzhen", "mou__lijian", "mou__huoji", "mou__quhu", "mou__luanji", "mou__duanliang",
      --overseas
      "os__zhuidu", "os__jimeng", "os__beini", "os__mutao", "os__yuejian", "os__bingde", "os__waishi", "os__daoji",
      "os__zhanyi",
      "os__juexing", "os__muyue", "os__weipo", "os__shangyi", "os__xuechang", "os__danlie",
      --jsrg
      "pingtao", "js__lianzhu", "shelun", "duxing", "jinmie", "lunshi", "xushiz", "yangming", "shili", "chushi",
      "js__jinfa",
      "zhuni", "daoren", "saojian", "yansha", "xiangjia",
      --mini
      "mini_xiangzhi", "mini_tongxin", "mini_yanshi", "mini_delu", "mini_zhujiu", "mini_zunbei", "mini_jifeng",
      --offline
      "miaojian", "chaos__lulue", "sgsh__daoshu", "ofl__jinfa", "ofl__zhanyan", "daohe", "zhiyiz", "xuepin",
      "ofl__shameng", "duyi",
      "mixin", "fuluan",
      --qsgs
      "qyt__jueji", "qyt__guihan", "qw__houyuan",
      --sanguokill
      "sk__hechun", "sk__choujue", "sk__limu", "sk__jiaozhao", "sk__wurong", "sk__taoxi", "sk__guiyuan", "sk__shajue",
      "sk__yanlie",
      "sk_le__jieyin", "sk__zhengnan",
    },
    ["damaged"] = {
      --standard_ex
      "ex__jianxiong", "ex__yiji", "ex__fankui", "ex__ganglie", "ty__wangxi",
      --shzl
      "ol_ex__jieming", "fangzhu", "ol_ex__beige", "guixin",
      --yj
      "os_ex__enyuan", "zhichi", "ty_ex__zhiyu", "ty_ex__chengxiang", "yuce", "m_ex__duodao", "ty_ex__shibei", "huituo",
      "qingxian",
      "ty_ex__yaoming",
      --sp
      "ty__jilei", --"fenyong",
      --OL
      "ty__benyu", "chouce", "yashi", "dingcuo", "liejie", "chuanwu", "shilu", "xibing", "zhongyun", "xieshu", "huanghan",
      --tenyear
      "weilu", "ty__baobian", "zhantao", "peiqi", "fumou", "xiace", "qianlong", "yuqi", "silun", "zhenxing", "bihuo",
      "shunshi", "lundao", "jijing", "yachai", "xieshou", "yijia", "jini", "wanggui", "kangli", "qiusuo",
      --mobile
      "rangjie", "jiebing", "xunde", "yiyongw", "difei", "xianchou",
      --overseas
      "os__yangshi", "os__liechi", "os__yimou", "os__fupan", "os__miaolue",
      --jsrg
      "js__rangjie", "dingce", "js__fengxiang", "jueyin",
      --offline
      "langgu",
      --qsgs
      "qyt__guixin", "qyt__chengxiang", "qyt__toudu", "qw__tanlan",
      --sanguokill
      "sk__fulu", "sk__danxin", "sk__xingshuai", "sk__kunfen", "sk__chenyu", "sk__jiexin",
    },
  }
  return table.filter(list[event] or {}, function(skill_name)
    return Fk.skills[skill_name] and not player:hasSkill(skill_name, true)
  end)
end


sk__yaozhi:addEffect("active", {
  name = "sk__yaozhi",
  prompt = "#sk__yaozhi",
  card_num = 0,
  target_num = 0,
  can_use = function(self, player)
    return player:getMark("sk__yaozhi_used-phase") == 0
  end,
  card_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    room:setPlayerMark(player, "sk__yaozhi_used-phase", 1)
    player:drawCards(1, sk__yaozhi.name)
    if player.dead then return end
    local skills = getYaozhiSkills(player, "phase_play")
    if #skills == 0 then return end
    local skill_name = room:askForChoice(player, table.random(skills, 3), sk__yaozhi.name, "#sk__yaozhi-choice", true)
    local phase_event = room.logic:getCurrentEvent():findParent(GameEvent.Phase)
    if phase_event ~= nil then
      room:handleAddLoseSkills(player, skill_name, nil, true, false)
      local mark = player:getTableMark("sk__yaozhi")
      table.insertIfNeed(mark, skill_name)
      room:setPlayerMark(player, "sk__yaozhi", mark)
      if not Fk.skills[skill_name]:isInstanceOf(ViewAsSkill) then
        room:askForUseActiveSkill(player, skill_name, "#sk__yaozhi-active:::" .. skill_name, true)
        room:handleAddLoseSkills(player, "-" .. skill_name, nil, true, false)
      end
      phase_event:addCleaner(function()
        room:handleAddLoseSkills(player, "-" .. skill_name, nil, true, false)
      end)
    end
  end,
})

sk__yaozhi:addEffect(fk.Damaged, {
  name = "#sk__yaozhi_trigger",
  mute = true,
  --sk__yaozhi,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sk__yaozhi.name) then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("sk__yaozhi")
    room:notifySkillInvoked(player, "sk__yaozhi", "special")
    player:drawCards(1, "sk__yaozhi")
    if player.dead then return end
    local skills = {}
    skills = getYaozhiSkills(player, "damaged")
    if #skills == 0 then return end
    local skill_name = room:askForChoice(player, table.random(skills, 3), "sk__yaozhi", "#sk__yaozhi-choice", true)
    local skill = Fk.skills[skill_name]
    if skill == nil then return end
    room:handleAddLoseSkills(player, skill_name, nil, true, false)
    local mark = player:getTableMark("sk__yaozhi")
    table.insertIfNeed(mark, skill_name)
    room:setPlayerMark(player, "sk__yaozhi", mark)
    if skill:triggerable(event, target, player, data) then
      skill:trigger(event, target, player, data)
    end
    room:handleAddLoseSkills(player, "-" .. skill_name, nil, true, false)
  end,
})
sk__yaozhi:addEffect(fk.EventPhaseStart, {
  name = "#sk__yaozhi_trigger",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sk__yaozhi.name) then
      return player.phase == Player.Start or player.phase == Player.Finish
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("sk__yaozhi")
    room:notifySkillInvoked(player, "sk__yaozhi", "special")
    player:drawCards(1, "sk__yaozhi")
    if player.dead then return end
    local skills = {}
    if player.phase == Player.Start then
      skills = getYaozhiSkills(player, "phase_start")
    elseif player.phase == Player.Finish then
      skills = getYaozhiSkills(player, "phase_finish")
    end
    if #skills == 0 then return end
    local skill_name = room:askForChoice(player, table.random(skills, 3), "sk__yaozhi", "#sk__yaozhi-choice", true)
    local skill = Fk.skills[skill_name]
    if skill == nil then return end
    room:handleAddLoseSkills(player, skill_name, nil, true, false)
    local mark = player:getTableMark("sk__yaozhi")
    table.insertIfNeed(mark, skill_name)
    room:setPlayerMark(player, "sk__yaozhi", mark)
    if skill:triggerable(event, target, player, data) then
      skill:trigger(event, target, player, data)
    end
    room:handleAddLoseSkills(player, "-" .. skill_name, nil, true, false)
  end,
})

return sk__yaozhi
